﻿using PLCS.Domain.Shared.Tasks;

using TaskStatus = PLCS.Domain.Shared.Tasks.TaskStatus;

namespace PLCS.Domain.Tasks;

public class TaskModel : BaseEntity
{
    /// <summary>
    /// 任务号
    /// </summary>
    public int TaskId { get; set; }

    /// <summary>
    /// 上层任务号
    /// </summary>
    public string? TaskId_Higher { get; set; }

    /// <summary>
    /// 工单号
    /// </summary>
    public string? OrderNo { get; set; }

    /// <summary>
    /// 托盘号
    /// </summary>
    public string? TrayNo { get; set; }

    /// <summary>
    /// 任务来源
    /// </summary>
    public string TaskSource { get; set; }

    /// <summary>
    /// 任务类型
    /// </summary>
    public TaskType TaskType { get; set; }

    /// <summary>
    /// 任务起始位（大的目标）
    /// </summary>
    public string? StartPos { get; set; }

    /// <summary>
    /// 任务目标位（大的目标）
    /// </summary>
    public string? EndPos { get; set; }

    /// <summary>
    /// 任务级别
    /// </summary>
    public int Level { get; set; }

    /// <summary>
    /// 途径工位（可以为空，也可以是一个或者多个，用‘，’隔开）
    /// </summary>
    public string? WayStation { get; set; }

    /// <summary>
    /// 托盘类型
    /// </summary>
    public TrayType TrayType { get; set; }

    /// <summary>
    /// 任务状态
    /// </summary>
    public TaskStatus TaskStatus { get;  set; }

    /// <summary>
    /// 任务前执行方法
    /// </summary>
    public string? BeforeExecutionMethod { get; set; }

    /// <summary>
    /// 任务前执行参数
    /// </summary>
    public string? BeforeExecutionParam { get; set; }

    /// <summary>
    /// 任务完成执行方法
    /// </summary>
    public string? AfterExecutionMethod { get; set; }

    /// <summary>
    /// 任务完成执行参数
    /// </summary>
    public string? AfterExecutionParam { get; set; }

    /// <summary>
    /// 备注
    /// </summary>
    public string? Remark { get; set; }

    /// <summary>
    /// 开始时间
    /// </summary>
    public DateTime? StartTime { get; set; }

    public ICollection<Instruction>? Instructions { get; set; }

    public TaskModel()
    {
    }

    public TaskModel(int taskId, int level, string startPos = null, string endPos = null, string? wayPoint = null, TaskType taskType = TaskType.普通流转, string taskSource = "手动创建", string? trayNo = null, string? orderNo = null, string? beforeExecutionMethod = null, string? beforeExecutionParam = null, string? afterExecutionMethod = null, string? afterExecutionParam = null)
    {
        TaskId = taskId;
        OrderNo = orderNo;
        Level = level;
        StartPos = startPos;
        EndPos = endPos;
        TaskType = taskType;
        TaskSource = taskSource;
        WayStation = wayPoint;
        TrayNo = trayNo;
        TaskStatus = TaskStatus.未执行;
        BeforeExecutionMethod = beforeExecutionMethod;
        AfterExecutionMethod = afterExecutionMethod;
        BeforeExecutionParam = beforeExecutionParam;
        AfterExecutionParam = afterExecutionParam;
    }

    public TaskModel SetUpperTaskId(string taskId_higer)
    {
        TaskId_Higher = taskId_higer;
        return this;
    }

    public TaskModel SetRemark(string remark)
    {
        Remark = remark;
        return this;
    }

    public void ChangeStatus(TaskStatus status)
    {
        TaskStatus = status;
        StartTime = DateTime.Now;
        if (status == TaskStatus.已完成)
        {
            AddLocalEvent(new TaskFinshEvent()
            {
                Id = Id,
                TaskId_Higher = TaskId_Higher,
                OrderNo = OrderNo,
                EndPos = EndPos,
                Level = Level,
                StartPos = StartPos,
                TaskSource = TaskSource,
                TaskType = TaskType,
                TaskId = TaskId,
                TrayType = TrayType,
                CreationTime = CreationTime,
                TrayNo = TrayNo
            });
        }
    }
}